﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace WPFDemo
{
    public class XmlReader
    {
        private static XmlDocument doc = new XmlDocument();
        private static string fileurl;
        //文件路径
        public static string FileUrl
        {
            set { fileurl = value; }
            get { return fileurl; }
        }
        /// <summary>
        /// 载入Xml文件
        /// </summary>
        /// <param name="url">Xml文件路径</param>
        public static void LoadXml(string url)
        {
            doc.Load(url);
        }
        /// <summary>
        /// 获取XmlDocument的根节点
        /// </summary>
        /// <returns>返回的XmlElement元素根节点</returns>
        public static XmlElement GetXmlDocumentRoot()
        {
            return doc.DocumentElement;
        }
        /// <summary>
        /// 获取指定元素的指定Attribute值
        /// </summary>
        /// <param name="xe">表示一个XmlElement</param>
        /// <param name="attr">表示Attribute的名字</param>
        /// <returns>返回获取的Attribute的值</returns>
        public static string GetAttribute(XmlElement xe, string attr)
        {
            return xe.GetAttribute(attr);
        }
        /// <summary>
        /// 获取指定节点的指定Attribute值
        /// </summary>
        /// <param name="xn">表示一个XmlNode</param>
        /// <param name="attr"></param>
        /// <returns>返回获取的Attribute的值</returns>
        public static string GetAttribute(XmlNode xn, string attr)
        {
            XmlElement xe = ExchangeNodeElement(xn);
            return xe.GetAttribute(attr);
        }
        /// <summary>
        /// XmlElement对象转换成XmlNode对象
        /// </summary>
        /// <param name="xe">XmlElement对象</param>
        /// <returns>返回XmlNode对象</returns>
        public static XmlNode ExchangeNodeElement(XmlElement xe)
        {
            return (XmlNode)xe;
        }
        /// <summary>
        /// XmlNode对象转换成XmlElement对象
        /// </summary>
        /// <param name="xe">XmlNode对象</param>
        /// <returns>返回XmlElement对象</returns>
        public static XmlElement ExchangeNodeElement(XmlNode xn)
        {
            return (XmlElement)xn;
        }
        /// <summary>
        /// 获取节点的文本
        /// </summary>
        /// <param name="xn"></param>
        /// <param name="nodename">节点的名称</param>
        /// <returns></returns>
        public static string GetXmlNodeInnerText(XmlNode xn, string nodename)
        {
            XmlNode childxn = xn.SelectSingleNode(nodename);
            return childxn.InnerText;
        }
        /// <summary>
        /// 获取指定节点的子节点
        /// </summary>
        /// <param name="xn">节点对象</param>
        /// <returns>返回子节点数</returns>
        public static int GetXmlNodeCount(XmlNode xn)
        {
            return xn.ChildNodes.Count;
        }
        /// <summary>
        /// 获取元素的文本
        /// </summary>
        /// <param name="xn">XmlElement元素</param>
        /// <param name="nodename">元素的名称</param>
        /// <returns></returns>
        public static string GetXmlElementInnerText(XmlElement xn, string nodename)
        {

            XmlNode childxn = xn.SelectSingleNode(nodename);
            return childxn.InnerText;
        }
        /// <summary>
        /// 获取XmlNode是否具有指定Attribute值
        /// </summary>
        /// <param name="xn">XmlNode对象</param>
        /// <param name="attr">Attribute的名称</param>
        /// <param name="compare">Attribute的值</param>
        /// <returns>返回bool值</returns>
        public static bool GetXmlNodeByArrtibute(XmlNode xn, string attr, string compare)
        {
            if (GetAttribute(xn, attr) == compare)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 获取XmlElement是否具有指定Attribute值
        /// </summary>
        /// <param name="xn">XmlElement对象</param>
        /// <param name="attr">Attribute的名称</param>
        /// <param name="compare">Attribute的值</param>
        /// <returns>返回bool值</returns>
        public static bool GetXmlNodeByArrtibute(XmlElement xe, string attr, string compare)
        {
            if (GetAttribute(xe, compare) == attr)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 获取一个具有指定Attrtibute的XmlNode子节点
        /// </summary>
        /// <param name="xn">XmlNode对象</param>
        /// <param name="attr">Attrtibute的名称</param>
        /// <param name="compare">Attrtibute的值</param>
        /// <returns>返回相应的子节点</returns>
        public static XmlNode GetXmlChildNodeByAttrtibute(XmlNode xn, string attr, string compare)
        {
            foreach (XmlNode cxn in xn.ChildNodes)
            {
                if (GetXmlNodeByArrtibute(cxn, attr, compare))
                {
                    return cxn;
                }
            }
            return null;
        }
    }
}
